import IM from './modules/IM'
import getter from './getters'
import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/utils/request'

Vue.use(Vuex)

const createStore = () => {
  return new Vuex.Store({
    state: () => ({
      deviceType: '', // 用户设备信息
      rcatAppInject: {},
      unique_id: '',
      baiduAK: '4my97GGVZZWhW3Or8hGGUAzfIdvhSDjd', // 百度地图AK
      isAlert: true,
      isFooter: false, // 控制脚部显示
      productAlert: false, // 控制商品跳APP弹框显示
      doctorAlert: false, // 控制医生跳APP弹框显示
      HospitalAlert: false, // 控制医院跳APP弹框显示
      UserAlert: false, // 控制用户跳APP弹框显示
      TopicAlert: false, // 控制话题跳APP弹框显示
      consultationAlert: false, // 控制私人定制页面APP弹框显示
      homeAlert: false, // 控制跳转App首页
      consultation: false, // 咨询弹框
      user_ip: '1'
    }),
    mutations: {
      SET_DEVICE_TYPE(state, payload) {
        state.deviceType = payload
      },
      SET_UNIQUE_ID(state, payload) {
        state.unique_id = payload
      },
      CLEAR_DATA(state) {
        state.deviceType = ''
        state.isAlert = false
        state.rcatAppInject = {}
      },
      SET_ISALERT(state) {
        state.isAlert = !state.isAlert
      },
      SET_ISFOOTER(state, payload) {
        if (payload) {
          state.isFooter = !state.isFooter
        } else {
          state.isFooter = payload
        }
      },
      SET_PRODUCTALERT(state) {
        state.productAlert = !state.productAlert
      },
      SET_DOCTORALERT(state) {
        state.doctorAlert = !state.doctorAlert
      },
      SET_HOSPITALALERT(state) {
        state.HospitalAlert = !state.HospitalAlert
      },
      SET_USERALERT(state) {
        state.UserAlert = !state.UserAlert
      },
      SET_TOPICALERT(state) {
        state.TopicAlert = !state.TopicAlert
      },
      SET_CONSULTATIONALERT(state) {
        state.consultationAlert = !state.consultationAlert
      },
      SET_HOMEALERT(state) {
        state.homeAlert = !state.homeAlert
      },
      SET_CONSULTATION(state) {
        state.consultation = !state.consultation
      },
      SET_IP(state, payload) {
        state.user_ip = payload
        // console.log(state.user_ip, 'vuex')
      }
    },
    actions: {
      nuxtServerInit({ commit }, { req, res }) {
        var url = require('url')
        const ip =
            req.headers['x-forwarded-for'] ||
            req.headers['x-real-ip'] ||
            req.connection.remoteAddress
        // res.cookie('user_ip', ip)
        var newId
        if (!req.cookies.unique_id) {
          newId = Number(Math.random().toString().substr(3, 10) + Date.now()).toString(36)
          commit('SET_UNIQUE_ID', newId)
          res.cookie('unique_id', newId, { expires: new Date(Date.now() + (24 * 60 * 60 * 1000 * 365)) })
        } else {
          commit('SET_UNIQUE_ID', req.cookies.unique_id)
        }

        var query = url.parse(req.url).query
        var _channel = ''
        if (query) {
          var vars = query.split('&')
          for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split('=')
            if (pair[0] === '_channel') { _channel = pair[1] }
          }
        }
        if (_channel) {
          request({
            url: `/client/collect`,
            method: 'get',
            params: {
              unique_id: req.cookies.unique_id,
              _channel
            }
          })
        } else {
          request({
            url: `/client/collect`,
            method: 'get',
            params: {
              unique_id: req.cookies.unique_id
            }
          })
        }

        commit('SET_IP', ip)
      },
      setDeviceType({ commit }, payload) {
        commit('SET_DEVICE_TYPE', payload)
      },
      clearData({ commit }) {
        commit('CLEAR_DATA')
      },
      // 是否显示跳App弹框
      setIsAlert({ commit }) {
        commit('SET_ISALERT')
      },
      // 是否显示脚部吸底
      setIsFooter({ commit }, payload) {
        commit('SET_ISFOOTER', payload)
      },
      // 控制商品的跳App
      setProductAlert({ commit }) {
        commit('SET_PRODUCTALERT')
      },
      // 控制医生的跳App
      setDoctorAlert({ commit }) {
        commit('SET_DOCTORALERT')
      },
      // 控制医院的跳App
      setHospitalAlert({ commit }) {
        commit('SET_HOSPITALALERT')
      },
      // 控制用户的跳APP
      setUserAlert({ commit }) {
        commit('SET_USERALERT')
      },
      // 控制话题的跳APP
      setTopicAlert({ commit }) {
        commit('SET_TOPICALERT')
      },
      // 控制私人定制页面APP弹框显示
      setConsultationAlert({ commit }) {
        commit('SET_CONSULTATIONALERT')
      },
      // 控制跳转App首页
      sethomeAlert({ commit }) {
        commit('SET_HOMEALERT')
      },
      // 控制咨询弹框
      setConsultation({ commit }) {
        commit('SET_CONSULTATION')
      }
    },
    modules: {
      IM
    },
    getters: getter
  })
}
// 客户端存储聊天记录
export default createStore
